﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GoogleVoice;
using System.Xml;
using System.IO;
using System.Threading;


namespace GVVVM
{
    public partial class GVVVM : Form
    {
        Silvermoon.Core.Screen screen;
        private MainMenu mainMenu;

        public bool UseWindowsTimer = true;

        public GVVVM()
        {
            InitializeComponent();

            // first of all, we need to create the one and only instance of the Screen to render the controls:
            screen = Silvermoon.Core.Screen.Initialize(this);

            // now we set the background color of the screen to white. Knowing this, we do not need to render background separately
            // if the background has the same color which results in faster rendering:
            screen.Renderer.BackgroundColor = Color.White;
            screen.Renderer.Perspective = true;

            // here we create the main window and show it:
            mainMenu = new MainMenu();
            mainMenu.Show();

            SetTimer();
        }

        #region Debug
        private void SetTimer()
        {
#if ShowTimer
            Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000 / 4;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
#endif
        }


#if ShowTimer
        void timer_Tick(object sender, EventArgs e)
        {
            float ticksPerFrame = screen.TicksPerFrame;
            if (ticksPerFrame == 0f) ticksPerFrame = 0.01f;
            float ms = 1000f / ticksPerFrame;
            Text = ms.ToString("F");
        }
#endif
        #endregion

        /// <summary>
        /// The only thing on paint we do is to signal the screen to invalidate it's content and to render immediately:
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (screen != null)
            {
                screen.Invalidate();
                screen.Render();
            }
        }

        /// <summary>
        /// To prevent flickering, we do nothing here.
        /// </summary>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

    }
}